var symbol = ['#', '(', '+', '-', '*', '/', ')'];
var symbolPriority = {
  '#': 0,
  '(': 1,
  '+': 2,
  '-': 2,
  '*': 3,
  '/': 3,
  ')': 4
}

function operaSymbol (char, sym, res) {
  let lastChar = sym[sym.length - 1];
  if (!lastChar) {
    sym.push(char);
  }
  if (char === '(') {
    sym.push(char);
  } else if (char === ')') {
    let currChar = sym.pop();
    while (sym && currChar !== '(') {
      res.push(currChar);
      currChar = sym.pop();
    }
  } else if (symbolPriority[char] > symbolPriority[lastChar]) {
    sym.push(char);
  } else if (symbolPriority[char] <= symbolPriority[lastChar]) {
    while (lastChar && (symbolPriority[char] <= symbolPriority[lastChar])) {
      let currChar = sym.pop();
      res.push(currChar);
      lastChar = sym[sym.length - 1];
    }
    sym.push(char);
  }
}

function toSuffixExpre (str) {
  let res = [];
  let sym = [];
  // 用于记录数字
  let substr = '';//数字>=10时遍历会记为两个数字
  for (let i = 0; i < str.length; i++) {
    // 判断是否是符号
    if (symbol.includes(str[i])) {
      substr && res.push(substr);
      substr = '';
      operaSymbol(str[i], sym, res);
    } else {
      substr += str[i];
    }
  }
  res.push(substr);
  while (sym.length > 0) {
    let currChar = sym.pop();
    res.push(currChar);
  }

  let result = res.join(' ');
  return result;
}

var test1 = '1+(2-3)*4+10/5'
console.log('the success is 1 2 3 - 4 * + 10 5 / +')
var result1 = toSuffixExpre(test1)
console.log(result1)
// var test2 = 'A+(B-C/D)*E'
// console.log('the success is ABCD/-E*+')
// var result2 = toSuffixExpre(test2)
// console.log(result2)
/**
 * @param {string[]} tokens
 * @return {number}
 */

var calc = function (s1, s2, char) {
  switch (char) {
    case "+": return s1 + s2;
    case "-": return s1 - s2;
    case "*": return s1 * s2;
    case "/": return (s1 / s2) <= 0 ? Math.ceil(s1 / s2) : Math.floor(s1 / s2);
  }
}
var evalRPN = function (tokens) {
  let stack = [],
    len = tokens.length;
  let res = null;
  for (let i = 0; i < len; i++) {
    if (tokens[i] === "+"
      || tokens[i] === "-"
      || tokens[i] === "*"
      || tokens[i] === "/") {
      let s2 = stack.pop(),
        s1 = stack.pop();
      res = calc(s1 * 1, s2 * 1, tokens[i]);
      stack.push(res);
    } else {
      stack.push(tokens[i]);
    }
  }
  return stack.pop();
};
console.log(evalRPN(result1));